import { Ads } from './ad';
let _Ads = Ads;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import AdMock from './mallm/pub/adMock';
 * AdMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mallm/pub/ad/listKey').reply(config => {
      let params = config.params;
      let mockAds = _Ads.filter(ad => {
      	if(params.id||params.id==''){ if( ad.id.indexOf(params.id)>=0) return true;}
      	if(params.adPositionId||params.adPositionId==''){ if( ad.adPositionId.indexOf(params.adPositionId)>=0) return true;}
      	if(params.mediaType||params.mediaType==''){ if( ad.mediaType.indexOf(params.mediaType)>=0) return true;}
      	if(params.name||params.name==''){ if( ad.name.indexOf(params.name)>=0) return true;}
      	if(params.link||params.link==''){ if( ad.link.indexOf(params.link)>=0) return true;}
      	if(params.endTime||params.endTime==''){ if( ad.endTime.indexOf(params.endTime)>=0) return true;}
      	if(params.enabled||params.enabled==''){ if( ad.enabled.indexOf(params.enabled)>=0) return true;}
      	if(params.imageUrl||params.imageUrl==''){ if( ad.imageUrl.indexOf(params.imageUrl)>=0) return true;}
      	if(params.deptid||params.deptid==''){ if( ad.deptid.indexOf(params.deptid)>=0) return true;}
      	if(params.content||params.content==''){ if( ad.content.indexOf(params.content)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockAds
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mallm/pub/ad/list').reply(config => {
      let params = config.params;
      let mockAds = _Ads.filter(ad => {
      	var isTrue=false;
      	if(params.id||params.id==''){if(ad.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.adPositionId||params.adPositionId==''){if(ad.adPositionId.indexOf(params.adPositionId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.mediaType||params.mediaType==''){if(ad.mediaType.indexOf(params.mediaType)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.name||params.name==''){if(ad.name.indexOf(params.name)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.link||params.link==''){if(ad.link.indexOf(params.link)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.endTime||params.endTime==''){if(ad.endTime.indexOf(params.endTime)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.enabled||params.enabled==''){if(ad.enabled.indexOf(params.enabled)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.imageUrl||params.imageUrl==''){if(ad.imageUrl.indexOf(params.imageUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.deptid||params.deptid==''){if(ad.deptid.indexOf(params.deptid)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.content||params.content==''){if(ad.content.indexOf(params.content)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockAds
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockAds.length;
      mockAds = mockAds.filter((ad, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockAds,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mallm/pub/ad/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _Ads = _Ads.filter(ad => ad.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mallm/pub/ad/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _Ads = _Ads.filter(ad => !params.includes(ad.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mallm/pub/ad/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { id,adPositionId,mediaType,name,link,endTime,enabled,imageUrl,deptid,content } = params;
      _Ads.some(ad => {
        if (ad.id === id) {
			ad.id = id;
			ad.adPositionId = adPositionId;
			ad.mediaType = mediaType;
			ad.name = name;
			ad.link = link;
			ad.endTime = endTime;
			ad.enabled = enabled;
			ad.imageUrl = imageUrl;
			ad.deptid = deptid;
			ad.content = content
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mallm/pub/ad/add').reply(config => {
      var params = JSON.parse( config.data );
      let { id,adPositionId,mediaType,name,link,endTime,enabled,imageUrl,deptid,content } = params;
      _Ads.push({
		id:id,
		adPositionId:adPositionId,
		mediaType:mediaType,
		name:name,
		link:link,
		endTime:endTime,
		enabled:enabled,
		imageUrl:imageUrl,
		deptid:deptid,
		content:content
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};